<template>
    <div id="tmpl">
        <div id="postcomment">
            <h3>提交评论</h3>
            <p></p>
            <textarea placeholder="请输入评论" v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发布</mt-button>
        </div>
        <div id="list">
            <h3>评论列表</h3>
            <p></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index+1}}楼</span>
                    <span>用户:{{item.user_name}}</span>
                    <span>发表时间:{{item.add_time | datefmt("YYYY-MM-DD hh:mm:ss")}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell">{{item.content}}</li>
                </ul>
            </div>
        </div>
        <mt-button plain type="danger" size="large" @click="getmore">加载更多</mt-button>
    </div>
</template>

<script>
    import { Toast } from 'mint-ui';
    import common from '../../kits/common.js';
    export default{
        data(){
            return{
                pageindex:1,
                postcontent:'',
                list:[
//                    {
//                        "user_name": "匿名用户",
//                        "add_time": "2017-07-04T09:58:23.000Z",
//                        "content": "00000"
//                    },
//                    {
//                        "user_name": "匿名用户",
//                        "add_time": "2017-07-04T08:46:47.000Z",
//                        "content": "我是你的好基友"
//                    },
//                    {
//                        "user_name": "匿名用户",
//                        "add_time": "2017-07-04T08:45:37.000Z",
//                        "content": ".........."
//                    }
                ]
            }
        },
        props:['id'],
        created(){
            this.getlist(this.pageindex)
        },
        methods:{
            getmore(){
                this.pageindex++;
                this.getlist(this.pageindex)
            },
            postcomment(){
                if(this.postcontent.trim().length <= 0){
                    Toast('输入不能为空');
                    this.postcontent = '';
                    return
                }
                var url = common.apidomain + '/api/postcomment/'+this.id;
                this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function(response){
                    var data = response.body;
                    Toast(data.message);
                    this.list = [ {
                        "user_name": "匿名用户",
                        "add_time": new Date(),
                        "content": this.postcontent
                    }].concat(this.list);
                    this.postcontent = '';
                })
            },
            getlist(pageindex){
                pageindex = pageindex || 1;
                var url = common.apidomain + '/api/getcomments/'+this.id+'?pageindex='+pageindex;
                this.$http.get(url).then(function(response){
                    var data = response.body;
                    if(data.status != 0){
                        Toast(data.message);
                        return;
                    }
                    this.list = this.list.concat(data.message);
                })
            }
        }
    }
</script>

<style scoped>
    #postcomment{
        padding: 5px;
    }
    #postcomment p{
        width: 100%;
        height: 1px;
        border-top: 1px solid #c0c0c0;
    }
    #list p{
        width: 100%;
        height: 1px;
        border-top: 1px solid #c0c0c0;
    }
    #list{
        padding: 5px;
    }
    .title{
        color: #6d6d72;
        font-size: 15px;
        background-color: #cccccc;
        padding: 5px;
    }
</style>